"""
登录验证装饰器
"""
import json
from user_abt.models import Session_Self
from return_data.return_data import return_data
from django.http import HttpResponse


def login_required(view_func):
    def wrapper(request, *view_args, **view_kwargs):
        if request.method == 'OPTIONS':
            return HttpResponse('200')
        else:
            # print(request.headers)
            sessionid = request.headers['sessionid']
            # print(sessionid)
            session_self = Session_Self.objects.filter(sessionid=sessionid).first()
            # print(f'zheli{session_self}')
            if session_self:
                return view_func(request, *view_args, **view_kwargs)
            else:
                message = '用户未登录'
                status = 4010
                ret = return_data(status, message, {})
                return HttpResponse(ret)
    return wrapper